#include "Sales_data.h"
#include <iostream>
#include <fstream>

int main(int argc, char *argv[])
{
    using namespace std;
    if (argc != 3)
    {
        cerr << "Enter filename!" << endl;
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "Could not open input file!" << endl;
        return -1;
    }

    ofstream out(argv[2]);
    if (!out)
    {
        cerr << "Could not open output file!" << endl;
    }

    Sales_data total;
    if (read_record(in, total))
    {
        Sales_data trans;
        while (read_record(in, trans))
        {
            if (total.isbn() == trans.isbn())
            {
                total.combine(trans);
            }
            else
            {
                print_record(out, total) << endl;
                total = trans;
            }
        }
        print_record(out, total);
    }
    else
    {
        cerr << "No data!" << endl;
    }

    return 0;
}
